home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 42 / Amiga Format AFCD42 (Issue 126, Aug 1999).iso / -serious- / graphics / h8jpg / src / h8jpg.s < prev    next >
Text File  |  1999-05-25  |  16KB  |  830 lines

  1.  
  2. ;
  3. ;
  4. ; h8jpg - High-Quality-HAM8 JPEG image viewer
  5. ;
  6. ; code by Smack/Infect!
  7. ;
  8. ;
  9. ; requirements:
  10. ;  -AGA, MC68020+, OS V39+
  11. ;  -jpeg.library V5+
  12. ;  -reqtools.library V38+
  13. ;
  14. ;
  15. ; Tue 18-May-99  V0.1
  16. ;
  17. ;
  18.     incdir    hd2:h8jpg/src/
  19.     include    os_macros.i
  20.  
  21.  
  22.     section    code,code
  23. first_
  24.     basereg    first_,a4
  25.     lea    (first_,pc),a4
  26. ;--------------------------------------
  27. ; libraries
  28.     move.l    (4).w,a6
  29.     move.l    (378,a6),a0    ;LibList
  30.     lea    (intuitionname,pc),a1
  31.     CALL    FindName
  32.     move.l    d0,(intuitionbase,a4)
  33.     move.l    (378,a6),a0    ;LibList
  34.     lea    (gfxname,pc),a1
  35.     CALL    FindName
  36.     move.l    d0,(gfxbase,a4)
  37.     move.l    (378,a6),a0    ;LibList
  38.     lea    (dosname,pc),a1
  39.     CALL    FindName
  40.     move.l    d0,(dosbase,a4)
  41.     move.l    d0,a6
  42.  
  43. ;--------------------------------------
  44. ; check system requirements
  45.     move.l    (4).w,a5
  46.     cmp    #39,(20,a5)    ;LIB_Version
  47.     blt    err_ende
  48.  
  49.     lea    (titletxt,pc),a0
  50.     move.l    a0,d1
  51.     CALL    PutStr
  52.  
  53.     move    (296,a5),d0    ;AttnFlags
  54.     and    #2,d0
  55.     beq.b    .syserr
  56.     move.l    (gfxbase,pc),a0
  57.     move.b    (236,a0),d0    ;gb_ChipRevBits0
  58.     and.b    #$0c,d0        ;GFXF_AA_Alice | GFXF_AA_Lisa
  59.     bne.b    .sysok
  60. .syserr
  61.     lea    (syserrtxt,pc),a0
  62.     move.l    a0,d1
  63.     CALL    PutStr
  64.     bra    err_ende
  65. .sysok
  66.  
  67. ;--------------------------------------
  68. ; jpeg library
  69.     moveq    #5,d0
  70.     lea    (jpegname,pc),a1
  71.     CALLEXEC OpenLibrary
  72.     move.l    d0,(jpegbase,a4)
  73.     bne.b    .jpeglib_ok
  74.  
  75.     lea    (jpegliberrtxt,pc),a0
  76.     move.l    a0,d1
  77.     CALLDOS    PutStr
  78.     bra    err_ende
  79. .jpeglib_ok
  80.  
  81. ;--------------------------------------
  82. ; read command line arguments
  83.     lea    (template,pc),a0
  84.     move.l    a0,d1
  85.     lea    (readargs_result,pc),a0
  86.     move.l    a0,d2
  87.     moveq    #0,d3
  88.     CALLDOS    ReadArgs
  89.     move.l    d0,(rdargs,a4)
  90.  
  91.     move.l    (readargs_result,pc),(filenames_array,a4)
  92.     bne    filenames_ok
  93.  
  94. ;--------------------------------------
  95. ; no filenames given
  96.     moveq    #38,d0
  97.     lea    (rtname,pc),a1
  98.     CALLEXEC OpenLibrary    ;V38 reqtools.library
  99.     move.l    d0,(rtbase,a4)
  100.     beq    ende
  101.  
  102.     moveq    #0,d0        ;RT_FILEREQ
  103.     lea    (tags_tagdone,pc),a0
  104.     CALLRT    rtAllocRequestA
  105.     move.l    d0,(filerequest,a4)
  106.     beq    err_ende
  107.  
  108.  
  109. dofilerequester
  110.     lea    (filereqtags,pc),a0
  111.     move.l    (filerequest,pc),a1
  112.     lea    (rtfilename,pc),a2
  113.     lea    (filereqtitle,pc),a3
  114.     CALLRT    rtFileRequestA
  115.     move.l    d0,(rtfilelist,a4)
  116.     beq    ende
  117.     move.l    d0,(rtfilelist_curr,a4)
  118.  
  119.     move.l    (filerequest,pc),a5
  120.     move.l    (16,a5),d1    ;filereq->dir
  121.     beq.b    .no_currdir_new
  122.     moveq    #-2,d2        ;SHARED_LOCK
  123.     CALLDOS    Lock
  124.     move.l    d0,d1
  125.     beq    err_ende
  126.  
  127.     move.l    d0,(currdir_lock,a4)
  128.     CALL    CurrentDir    ;set current dir
  129.     move.l    d0,(currdir_old,a4)
  130. .no_currdir_old
  131.  
  132.     lea    (currdirtxt,pc),a0
  133.     move.l    a0,d1
  134.     lea    (vprintf_long1,pc),a0
  135.     move.l    (16,a5),(a0)
  136.     move.l    a0,d2
  137.     CALL    VPrintf
  138. .no_currdir_new
  139.  
  140. filenames_ok
  141.  
  142. ;--------------------------------------
  143. ; open intermediate screen
  144.     move.l    (dummyscreen,pc),d0
  145.     bne.b    .no_dummy
  146.     lea    (dummynewscreen,pc),a0
  147.     lea    (screentaglist,pc),a1
  148.     CALLINT    OpenScreenTagList
  149.     move.l    d0,(dummyscreen,a4)
  150. .no_dummy
  151.  
  152. ;--------------------------------------
  153. ; open file
  154. files_loop
  155.     clr    (exit,a4)
  156.     moveq    #1,d0
  157.     move.l    d0,(scale_down_factor,a4)
  158.  
  159.     lea    (linefeedtxt,pc),a0
  160.     move.l    a0,d1
  161.     CALLDOS    PutStr
  162.  
  163.     move.l    (rtfilelist,pc),d0
  164.     beq.b    .no_rtfiles
  165.     lea    (rtfilelist_curr,pc),a0
  166.     move.l    (a0),d0
  167.     beq    ende
  168.     move.l    d0,a1
  169.     move.l    (a1),(a0)    ;rtfl_Next
  170.     move.l    (8,a1),d1    ;rtfl_Name
  171.     bra.b    .open_file
  172. .no_rtfiles
  173.     move.l    (filenames_array,pc),a0
  174.     move.l    (a0)+,d1
  175.     beq    ende
  176.     move.l    a0,(filenames_array,a4)
  177. .open_file
  178.     move.l    d1,(filename,a4)
  179.  
  180.     lea    (filenametxt,pc),a0
  181.     move.l    a0,d1
  182.     lea    (filename,pc),a0
  183.     move.l    a0,d2
  184.     CALL    VPrintf
  185.  
  186.     move.l    (filename,pc),d1
  187.     move.l    #1005,d2    ;MODE_OLDFILE
  188.     CALL    Open
  189.     move.l    d0,(fh,a4)
  190.     bne.b    .fileopen_ok
  191.  
  192.     lea    (fileopenerrtxt,pc),a0
  193.     move.l    a0,d1
  194.     CALL    PutStr
  195.     bra.b    files_loop
  196. .fileopen_ok
  197.  
  198. ;--------------------------------------
  199. ; alloc JPEG decompress
  200.     lea    (jph,pc),a0
  201.     lea    (ajd_taglist,pc),a1
  202.     move.l    (fh,pc),(4,a1)
  203.     move.l    (jpegbase,pc),a6
  204.     jsr    (-30,a6)    ;AllocJPEGDecompressA()
  205.     tst.l    d0
  206.     beq.b    .ajd_ok
  207.  
  208.     cmp.l    #14,d0        ;JPGERR_NOTJPEG
  209.     bne.b    .not_err14
  210.     lea    (notjpegerrtxt,pc),a0
  211.     move.l    a0,d1
  212.     CALLDOS    PutStr
  213.     bra    close_file
  214. .not_err14
  215.     lea    (allocjpegerrtxt,pc),a0
  216.     move.l    a0,d1
  217.     lea    (vprintf_long1,pc),a0
  218.     move.l    a0,d2
  219.     move.l    d0,(a0)
  220.     CALLDOS    VPrintf
  221.     bra    close_file
  222. .ajd_ok
  223.  
  224. ;--------------------------------------
  225. ; get JPEG info
  226. retry_smaller
  227.     move.l    (jph,pc),a0
  228.     lea    (gji_taglist,pc),a1
  229.     move.l    (scale_down_factor,pc),(gji_scale,a4)
  230.     move.l    (jpegbase,pc),a6
  231.     jsr    (-78,a6)    ;GetJPEGInfoA()
  232.     tst.l    d0
  233.     beq.b    .gji_ok
  234.  
  235.     lea    (jpeginfoerrtxt,pc),a0
  236.     move.l    a0,d1
  237.     lea    (vprintf_long1,pc),a0
  238.     move.l    a0,d2
  239.     move.l    d0,(a0)
  240.     CALLDOS    VPrintf
  241.     bra    free_jph
  242. .gji_ok
  243.     lea    (jpeginfotxt,pc),a0
  244.     move.l    a0,d1
  245.     lea    (vprintf_long1,pc),a0
  246.     move.l    a0,d2
  247.     move.l    (width,pc),(a0)+
  248.     move.l    (height,pc),(a0)
  249.     CALLDOS    VPrintf
  250.  
  251.     move.b    (colourspace,pc),d1
  252.     subq.b    #1,d1
  253.     bne.b    .not_gray
  254.     lea    (grayerrtxt,pc),a0
  255.     move.l    a0,d1
  256.     CALL    PutStr
  257.     bra    free_jph
  258. .not_gray
  259.  
  260. ;--------------------------------------
  261. ; fit image to screen
  262.     move.l    (fitscreen,pc),d0
  263.     beq.b    .no_fitscreen
  264.     move.l    (scale_down_factor,pc),d0
  265.     subq.l    #1,d0
  266.     bne.b    .no_fitscreen
  267.  
  268.     move.l    (width,pc),d0
  269.     move    #352,d1        ;screen width (LoRes visible)
  270.     divu    d1,d0
  271.     swap    d0
  272.     move.l    (height,pc),d2
  273.     move    #280,d3        ;screen height
  274.     divu    d3,d2
  275.     swap    d2
  276.  
  277.     cmp.l    d0,d2
  278.     ble.b    .max_d0
  279.     move.l    d2,d0
  280.     move    d3,d1
  281. .max_d0
  282.     lsr    #1,d1
  283.     sub    d1,d0        ;remainder > 1/2 divisor ?
  284.     ble.b    .quot_ok
  285.     add.l    #$10000,d0    ;increment quotient
  286. .quot_ok
  287.     swap    d0
  288.     moveq    #0,d1
  289.     move.b    (.fit_table,pc,d0),d1
  290.     cmp.b    #1,d1
  291.     beq.b    .no_fitscreen
  292.  
  293.     lea    (scale_down_factor,pc),a0
  294.     move.l    a0,d2
  295.     move.l    d1,(a0)
  296.     lea    (fitscreentxt,pc),a0
  297.     move.l    a0,d1
  298.     CALLDOS    VPrintf
  299.     bra    retry_smaller
  300.  
  301. .fit_table
  302.     dc.b    1,1,2,4,4,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
  303. .no_fitscreen
  304.  
  305. ;--------------------------------------
  306. ; allocate chip memory
  307.     move.l    (width,pc),d0
  308.     lsl.l    #2,d0
  309.     add.l    #63,d0
  310.     and.l    #-64,d0
  311.     move.l    d0,(width64,a4)
  312.  
  313.     move.l    (height,pc),d1
  314.     mulu    d1,d0
  315.     lsr.l    #3,d0
  316.     add.l    #63,d0
  317.     and.l    #-64,d0
  318.     move.l    d0,(bitplanesize,a4)
  319.  
  320.     move.l    d0,d7
  321.     lsl.l    #2,d0
  322.     add.l    d7,d0
  323.     add.l    d7,d0        ;*6
  324.     move.l    d0,(chipmem_6size,a4)
  325.     move.l    #$00050002,d1    ;MEMF_CHIP|MEMF_CLEAR|MEMF_REVERSE
  326.     CALLEXEC AllocMem
  327.     move.l    d0,(chipmem_6pt,a4)
  328.     beq.b    .no_chipmem
  329.  
  330.     move.l    d7,d0
  331.     add.l    d0,d0        ;*2
  332.     move.l    d0,(chipmem_2size,a4)
  333.     move.l    #$00040002,d1    ;MEMF_CHIP|MEMF_REVERSE
  334.     CALL    AllocMem
  335.     move.l    d0,(chipmem_2pt,a4)
  336.     bne.b    .chipmem_ok
  337. .no_chipmem
  338.     lea    (chipmem_6pt,pc),a0
  339.     move.l    (a0),d0
  340.     beq.b    .no_chip6
  341.     clr.l    (a0)
  342.     move.l    d0,a1
  343.     move.l    (chipmem_6size,pc),d0
  344.     CALL    FreeMem
  345. .no_chip6
  346.     lea    (chipmemerrtxt,pc),a0
  347.     move.l    a0,d1
  348.     lea    (vprintf_long1,pc),a0
  349.     move.l    a0,d2
  350.     move.l    (bitplanesize,pc),d0
  351.     lsr.l    #7,d0
  352.     move.l    d0,(a0)
  353.     CALLDOS    VPrintf
  354.  
  355.     lea    (scale_down_factor,pc),a0
  356.     move.l    (a0),d0
  357.     cmp.b    #8,d0
  358.     bge    free_chipmem
  359.     lsl.l    #1,d0
  360.     move.l    d0,(a0)
  361.     bra    retry_smaller
  362.  
  363. .chipmem_ok
  364.  
  365. ;--------------------------------------
  366. ; create bitmap structure
  367.     lea    (bmap,pc),a0
  368.     move.l    (width64,pc),d0
  369.     lsr.l    #3,d0
  370.     move    d0,(a0)+    ;BytesPerRow
  371.     move.l    (height,pc),d0
  372.     move    d0,(a0)+    ;Rows
  373.     clr.b    (a0)+        ;Flags
  374.     move.b    #8,(a0)+    ;Depth
  375.     clr    (a0)+        ;pad
  376.  
  377.     move.l    (chipmem_6pt,pc),d0
  378.     move.l    (bitplanesize,pc),d1
  379.     move.l    d0,(a0)+    ;Plane 0
  380.     add.l    d1,d0
  381.     move.l    d0,(a0)+    ;Plane 1
  382.     add.l    d1,d0
  383.     move.l    d0,(a0)+    ;Plane 2
  384.     add.l    d1,d0
  385.     move.l    d0,(a0)+    ;Plane 3
  386.     add.l    d1,d0
  387.     move.l    d0,(a0)+    ;Plane 4
  388.     add.l    d1,d0
  389.     move.l    d0,(a0)+    ;Plane 5
  390.     move.l    (chipmem_2pt,pc),d0
  391.     move.l    d0,(a0)+    ;Plane 6
  392.     add.l    d1,d0
  393.     move.l    d0,(a0)        ;Plane 7
  394.  
  395. ;--------------------------------------
  396. ; open screen and window
  397.     lea    (newscreen,pc),a0
  398.     move.l    (width,pc),d0
  399.     lsl    #2,d0
  400.     move    d0,(4,a0)
  401.     move.l    (height,pc),d0
  402.     move    d0,(6,a0)
  403.     lea    (screentaglist,pc),a1
  404.     CALLINT    OpenScreenTagList
  405.     move.l    d0,(screen,a4)
  406.     bne.b    .screen_ok
  407. .screrr    lea    (screenerrtxt,pc),a0
  408.     move.l    a0,d1
  409.     CALLDOS    PutStr
  410.     bra    close_win
  411. .screen_ok
  412.     lea    (newwindow,pc),a0
  413.     move.l    (screen,pc),a1
  414.     move.l    a1,(30,a0)
  415.     move.l    (12,a1),(4,a0)
  416.     lea    (windowtaglist,pc),a1
  417.     CALL    OpenWindowTagList
  418.     move.l    d0,(window,a4)
  419.     beq.b    .screrr
  420.  
  421.     move.l    d0,a5
  422.     CALLEXEC Disable
  423.     move.l    (86,a5),a5    ;window.wd_UserPort
  424.     move.b    #1,(14,a5)    ;window.wd_UserPort->mp_Flags=PA_SOFTINT
  425.     lea    (idcmp_softint,pc),a1
  426.     move.l    a1,(16,a5)    ;window.wd_UserPort->mp_SigTask/mp_SoftInt
  427.     CALL    Enable
  428.  
  429. ;--------------------------------------
  430. ; prepare HAM8 buffer
  431.     move.l    (chipmem_2pt,pc),a0
  432.     move.l    (bitplanesize,pc),d6
  433.     lsr.l    #3,d6
  434.     move.l    d6,d7
  435.     move.l    #$77777777,